package com.xuecheng.auth.controller;

import com.baomidou.mybatisplus.extension.api.R;
import com.xuecheng.base.model.RestResponse;
import com.xuecheng.ucenter.mapper.XcUserMapper;
import com.xuecheng.ucenter.model.dto.RegisterParamsDto;
import com.xuecheng.ucenter.model.po.XcUser;
import com.xuecheng.ucenter.service.LoginService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * @author Mr.M
 * @version 1.0
 * @description 测试controller
 * @date 2022/9/27 17:25
 */
@Slf4j
@RestController
@RequiredArgsConstructor
public class LoginController {

    @Autowired
    XcUserMapper userMapper;

    private final LoginService loginService;


    @RequestMapping("/login-success")
    public String loginSuccess() {

        return "登录成功";
    }


    @RequestMapping("/user/{id}")
    public XcUser getuser(@PathVariable("id") String id) {
        XcUser xcUser = userMapper.selectById(id);
        return xcUser;
    }

    @PreAuthorize("hasAuthority('p1')")//拥有p1权限方可访问
    @RequestMapping("/r/r1")
    public String r1() {
        return "访问r1资源";
    }

    @PreAuthorize("hasAuthority('p2')")//拥有p2权限方可访问
    @RequestMapping("/r/r2")
    public String r2() {
        return "访问r2资源";
    }

    @PostMapping("/register")
    public RestResponse register(@RequestBody RegisterParamsDto registerParamsDto) {
        log.info("注册参数为:{}", registerParamsDto);
        loginService.register(registerParamsDto);
        return RestResponse.success();
    }


}
